package com.graduation.fq.teacher.process;

import com.graduation.fq.teacher.module.CourseInfo;
import com.graduation.fq.teacher.utils.Const;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jifang
 * @version 2016/5/23.
 */
public class CourseListGetProcess extends BaseProcess {
    private String url = "/course/get_course_by_t_id.do?";
    private String id;
    private List<CourseInfo> courseInfos;

    public List<CourseInfo> getCourseInfos() {
        return courseInfos;
    }

    public void setCourseInfos(List<CourseInfo> courseInfos) {
        this.courseInfos = courseInfos;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    String getRequestUrl() {
        return new StringBuilder(url).append("t_id=").append(id).toString();
    }

    @Override
    String getParameter() {
        return null;
    }

    @Override
    void onResult(JSONObject object) {
        int statusCode = object.optInt("status");
        setStatus(statusCode);
        if (statusCode == Const.HTTP_SUCCESS) {
            JSONArray array = object.optJSONArray("data");
            JSONObject courseObject;
            courseInfos = new ArrayList<>();
            CourseInfo info;
            try {

                for (int i = 0; i < array.length(); i++) {
                    courseObject = array.getJSONObject(i);
                    info = new CourseInfo();
                    info.setId(courseObject.optString("id"));
                    info.setTeacherId(courseObject.optString("t_id"));
                    info.setCredit(courseObject.optInt("credit"));
                    info.setName(courseObject.optString("name"));
                    info.setTeacherName(courseObject.optString("teacher_name"));

                    courseInfos.add(info);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
